package com.xiaoyu.tool.messageQueue.sender;

import com.xiaoyu.tool.messageQueue.config.QueueProperties;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * rabbitmq配置
 * */
@EnableConfigurationProperties(QueueProperties.class)
@Configuration
public class ExpireAndDeleteOrderSender {

    private final RabbitTemplate expireAndDeleteOrderDelayTemplate;

    private final QueueProperties queueProperties;

    public ExpireAndDeleteOrderSender(RabbitTemplate expireAndDeleteOrderDelayTemplate, QueueProperties queueProperties) {
        this.expireAndDeleteOrderDelayTemplate = expireAndDeleteOrderDelayTemplate;
        this.queueProperties = queueProperties;
    }

    /**
     * 检查订单是否过期  一天
     * 取消
     * */
    public void sendCheckOrderExpiredAndDelete(String orderNo){
        expireAndDeleteOrderDelayTemplate.convertAndSend("expireAndDeleteOrderDelayExchange",
                queueProperties.getExpireAndDeleteOrderDelay(), orderNo,
                    new MessagePostProcessor() {
                        @Override
                        public Message postProcessMessage(Message message) throws AmqpException {
                            message.getMessageProperties().setHeader("x-delay", 1000 * 60 * 60 * 24);
                            return message;
                        }
                    });
    }
}
